﻿@using Microsoft.JSInterop
@using Covenant.Models.Grunts
@using Covenant.Components.Shared
@inject IJSRuntime IJSRuntime

<CovenantTable TItem="EmbeddedResource" Items="EmbeddedResources.ToList()" SearchRow="IsRowDisplayed"
               IsSearchable="IsSearchable" IsSortable="IsSortable" IsPaginated="IsPaginated" PageLength="PageLength" class="table table-hover">
    <TableHeader>
        <SortableTableHeader TItem="EmbeddedResource" TItem2="string" GetSortableProperty="(resource) => resource.Name">
            <HeaderContent>Name</HeaderContent>
        </SortableTableHeader>
        <SortableTableHeader TItem="EmbeddedResource" TItem2="string" GetSortableProperty="(resource) => resource.Location">
            <HeaderContent>Location</HeaderContent>
        </SortableTableHeader>
    </TableHeader>
    <TableRow Context="resource">
        <tr>
            <td>
                <a href="/embeddedresource/edit/@resource.Id">
                    @resource.Name
                </a>
            </td>
            <td>@resource.Location</td>
        </tr>
    </TableRow>
</CovenantTable>

@code {
    [Parameter]
    public IEnumerable<EmbeddedResource> EmbeddedResources { get; set; }

    [Parameter]
    public bool IsSearchable { get; set; } = false;

    [Parameter]
    public bool IsSortable { get; set; } = true;

    [Parameter]
    public bool IsPaginated { get; set; } = true;

    [Parameter]
    public int PageLength { get; set; } = 25;

    private bool IsRowDisplayed(EmbeddedResource resource, string SearchTerm)
    {
        return SearchTerm == string.Empty ||
            resource.Name.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase) ||
            resource.Location.Contains(SearchTerm, StringComparison.CurrentCultureIgnoreCase);
    }
}
